package ControllerInsert

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"test/ControllerConnct"
	"test/middleware"
	"test/models"
	"test/utils"
)

func Insert() {
	//连接数据库
	/*
		var db interface{} = 0
		db = connection
		fmt.Println(db)
	*/
	db := ControllerConnct.Connection()

	r := gin.Default()
	r.Use(middleware.CORSMiddleware())

	var students models.STUDENT

	//定义接受前端数据的变量
	var studentno string
	var studentname string
	var classno string
	var studentsex string
	var deptno string
	var collegename string

	//数据输入

	r.POST("/database/MB-TI", func(c *gin.Context) {
		studentno = c.PostForm("StudentNo")
		studentname = c.PostForm("StudentName")
		classno = c.PostForm("ClassNo")
		studentsex = c.PostForm("StudentSex")
		deptno = c.PostForm("DeptNo")
		collegename = c.PostForm("CollegeName")

		fmt.Println(studentno)
		fmt.Println(studentname)
		fmt.Println(classno)
		fmt.Println(studentsex)
		fmt.Println(deptno)
		fmt.Println(collegename)

		//绑定数据
		err := c.ShouldBindJSON(&students)
		if err != nil {
			fmt.Println(err.Error())
		}
		//students := models.Student{
		//	StudentNo:   studentno,
		//	StudentName: studentname,
		//	ClassNo:     classno,
		//	StudentSex:  studentsex,
		//	DeptNo:      deptno,
		//	CollegeName: collegename,
		//}
		db.Create(&students)

		c.JSON(http.StatusOK, utils.Result{
			Code:    utils.SUCCESS,
			Message: "Welcome to Myers–Briggs Type Indicator",
		})
	})
	//id := context.Query("id")
	//fmt.Println(id)

	err := r.Run(":8099")
	if err != nil {
		return
	}

	//print(db)

	//	接收数据并且插入

}
